<?php
namespace app\index\model;

use think\Model;

class Article extends Model
{
    // Category 数据对象
    protected $category = null;
    //格式时间格式
    protected $dateFormat = 'Y年m月d日 H:i:s';

    /*public function _initialize()
    {
        parent::_initialize();
        if ($this->category == null) $this->category = new \app\common\model\Category();
    }*/

    /**
     * 文章列表方法
     * @param int|array $cate_id 栏目id
     * @return \think\Paginator
     */
    public function article_list($cate_id = 0)
    {
        //首页列表
        $where = '1 = 1';
        if ($cate_id) {
            if (is_array($cate_id)) {
                //数组类型为 IN的子查询
                $where = ['category_id' => ['IN', $cate_id]];
            }else {
                $where = "category_id = $cate_id";
            }
        }
        $data = $this->field('id,title,description,thumb_url,editor,create_time,update_time')->where($where)->order('id desc')->paginate(8);
        return $data;
    }

    /**
     * 取出面包屑导航 和 对应栏目下面的所有文章
     * @param int $cate_id  栏目id
     * @return bool|array
     */
    public function cate_list($cate_id = 0)
    {
        if ($this->category == null) $this->category = new \app\common\model\Category();
        //1.取出栏目的面包屑导航
        $cate_list = $this->category->cate_list();
        // 数据为空，直接返回
        if (empty($cate_list)) return false;
        //$cate_list = collection($cate_list)->toArray(); //助手函数转换为数组
        $data['cate_parent_list'] = $this->getParents($cate_list, $cate_id);

        $ids = $this->getSonIds($cate_list, $cate_id);
        $ids[] = (int)$cate_id;

        //2. 取出栏目下面的 文章 --- 新增 ==> 当前栏目子栏目的文章
        $data['lists'] = $this->article_list($ids);
        return $data;
    }

    /**
     * 提供子ID返回父级栏目 用于：面包屑导航
     * @param $cate
     * @param $id
     * @param string $parent_id
     * @param string $primaryKey
     * @return array
     */
    public function getParents($cate, $id, $parent_id = 'parent_id',$primaryKey = 'id'){
        $arr = array();
        foreach($cate as $v){
            if($v->$primaryKey == $id){
                $arr[] = $v;
                $arr = array_merge($this->getParents($cate, $v->$parent_id, $parent_id, $primaryKey), $arr);
            }
        }
        return $arr;
    }

    /**
     * 提供子ID返回子级栏目id号 用于：查询某个栏目下面的所有文章
     * @param $cate
     * @param $id
     * @param string $parent_id
     * @param string $primaryKey
     * @return array
     */
    public function getSonIds($cate, $id, $parent_id = 'parent_id', $primaryKey = 'id')
    {
        $ids = [];
        foreach ($cate as $v)
        {
            if ($v[$parent_id] == $id)
            {
                $ids[] = $v[$primaryKey];
                $ids = array_merge($this->getSonIds($cate, $v[$primaryKey], $parent_id, $primaryKey), $ids);
            }
        }
        return $ids;
    }

    /**
     * 搜索功能简单实现
     * @param $keywords             //关键词
     * @return \think\Paginator
     */
    public function search($keywords)
    {
        $keywords = '%'.$keywords . '%';
        $list = $this->field('id,title,description,thumb_url,editor,create_time,update_time')->where('title LIKE :title OR description LIKE :desc OR content LIKE :content ', ['title' => $keywords, 'desc' => $keywords, 'content' => $keywords])->order('id desc ')->paginate(8);
        return $list;
    }
}